import Mock from 'mockjs'

// 学生信息列表
let { studentList } = Mock.mock({
    'studentList|450': [{
        'class|1': ['2210', '2211', '2212', '2213', '2214', '2215'],
        'type|1': ['离散分析', '线性代数', '微积分', '高等数学', '前端HTML5'],
        'stuId|+1': 1000,
        'stuNickname': '@ctitle(3, 5)',
        'stuName|1': '@cname()',
        'stuTel': /^(?:(?:))?1[3-9]\d{9}$/,
        // 完成度
        'plan|0-100': 1,
        'stuImg': '@image("80x80")',
        'stuSex|1': ['男', '女'],
        'stuSchool': '千锋教育',
        'stuEducation|1': ['博士', '硕士', '本科', '专科'],
        'stuAddress': '@county(true)',
        // 个性
        'stuPersonality': '@cparagraph(1,3)',
        // 备注
        'stuRemarks': '',
        'stuGrade': {
            '离散分析|30-100': 100,
            '线性代数|30-100': 100,
            '微积分|30-100': 100,
            '高等数学|30-100': 100,
            '前端HTML5|30-100': 100,
        },
        'stuPreGrade|40-95': 1,
        'isArrive|1': ['已出勤', '缺勤'],
        // 互动频次
        'interaction|0-350': 1,
        // 听课时长
        'listening|0-1000': 1000,
        // 是否点赞
        'isApproval': false,
    }]
});
for (let item of studentList) {
    // 在随机的数后面加上 %
    item.plan = item.plan + '%';
    // 将随机数转成字符串好用于之后的验证
    item.stuId = String(item.stuId);
    // 添加平均成绩
    let grades = item.stuGrade;
    let grade = 0;
    for (let v of Object.values(grades)) {
        grade += v
    };
    item.nowGrade = grade / 5;
}
// studentList.map(item => {
//     // 在随机的数后面加上 %
//     return item.plan = item.plan + '%';
// });
// studentList.map(item => {
//     // 将随机数转成字符串好用于之后的验证
//     return item.stuId = String(item.stuId);
// });

// 获取学生列表的接口
Mock.mock('/getStudentList', 'get', (config) => {

    let data = JSON.parse(config.body);
    let newData = studentList;
    let code = '200';
    let msg = '没有匹配的学生';
    if (data.type) {
        let flag1 = true;
        newData = newData.filter((item) => {
            flag1 = data.type == item.type;
            return flag1
        });
        if (newData.length == 0) {
            code = '1001';
            msg = '没找到对应的科目';
        } else {
            code = '200';
            msg = '请求成功';
        }
    }
    if (data.className) {
        let flag2 = true;
        newData = newData.filter((item) => {
            flag2 = data.className == item.class;
            return flag2
        });
        if (newData.length == 0) {
            code = '1001';
            msg = '没找到对应的班级';
        } else {
            code = '200';
            msg = '请求成功';
        }
    }
    if (data.id) {
        let flag3 = true;
        newData = newData.filter((item) => {
            if (data.id == item.stuId) {
                flag3 = true;
            } else if (data.id == item.stuNickname) {
                flag3 = true;
            } else {
                flag3 = false;
            }
            return flag3
        });
        if (newData.length == 0) {
            code = '1001';
            msg = '没找到对应的id或昵称';
        } else {
            code = '200';
            msg = '请求成功';
        }
    }
    return {
        code: code,
        msg: msg,
        data: newData
    }
});

// 添加学生备注

Mock.mock('/addRemarks', 'post', (config) => {
    let data = JSON.parse(config.body);
    let code = '1002';
    let msg = '你输入的内容为空';
    if (data.remark) {
        for (let item of studentList) {
            if (data.id == item.stuId) {
                code = '200';
                msg = '备注修改成功';
                item.stuRemarks = data.remark;
            }
        }
    };
    return {
        code,
        msg,
    }
});


// 题目推送页面中的数据接口
// 
// 所有的题目数据
let { questions } = Mock.mock({
    'questions|120': [{
        'quesId|+1': 100000,
        'quesContent': '@cparagraph(3,5)',
        'quesFrom': '@csentence(5,10)',
        // 正确率
        'probability|0-100': 1,
        'quesType|1': ['离散分析', '线性代数', '微积分', '高等数学', '前端HTML5'],
        'isSelect': false
    }]
});
for (let item of questions) {
    // 在正确率后面加上 %
    item.probability = item.probability + '%';
}
// 添加题目
Mock.mock('/getQues', 'get', (config) => {
    let data = JSON.parse(config.body);
    let newData = questions;
    let code = '200';
    let msg = '请求成功';
    if (data.quesType) {
        let flag1 = true;
        newData = newData.filter((item) => {
            flag1 = data.quesType == item.quesType;
            return flag1
        });
        if (newData.length == 0) {
            code = '1001';
            msg = '没找到对应的科目';
        }
    }
    if (data.min && data.max) {
        let flag2 = true;
        newData = newData.filter((item) => {
            flag2 = parseInt(item.probability) >= data.min && parseInt(item.probability) <= data.max;
            return flag2
        });
        if (newData.length == 0) {
            code = '1001';
            msg = '没找到对应的班级';
        }
    }

    if (data.num) {
        newData = newData.slice(0, data.num);
    }
    return {
        code: code,
        msg: msg,
        data: newData
    }
});
Mock.mock('/getQuesById', 'get', (config) => {
    let data = JSON.parse(config.body);
    let newData = questions;
    let code = '200';
    let msg = '请求成功';

    let flag3 = true;
    newData = newData.filter((item) => {
        flag3 = Number(data.quesId) == item.quesId;
        return flag3
    });
    if (newData.length == 0) {
        code = '1001';
        msg = '没找到对应的id或昵称';
    }
    return {
        code: code,
        msg: msg,
        data: newData
    }
})